﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Acr.XamForms.UserDialogs;
using Xamarin.Forms;
using Xamarin.Forms.Labs;

namespace LiveMakkah.Pages
{
    public class ChannelListPage : BasePage
    {
        private ListView listView;

        public ChannelListPage()
        {
            var top = new Image { Aspect = Aspect.AspectFit, Source = ImageSource.FromFile("headersized.jpg") };
            // var bottom = new Image {Aspect = Aspect.AspectFit, Source = ImageSource.FromFile("bottom.jpg")};

            listView = new ListView
            {
                // Define template for displaying each item (Argument of DataTemplate constructor is called for each item; it must return a Cell derivative.)
                ItemTemplate = new DataTemplate(() =>
                {
                   // var loadingImg = new Image { Aspect = Aspect.AspectFit, Source = ImageSource.FromFile("loading.png") };

                    // Create views with bindings for displaying each property.
                    var titleLabel = new Label();
                    titleLabel.SetBinding(Label.TextProperty, "Value1");
                    titleLabel.Font = Font.SystemFontOfSize(25, FontAttributes.Bold /*| FontAttributes.Italic*/);
                    titleLabel.TextColor = Color.FromHex("FFFFAC"); //Color.FromHex("5b3721");
                    titleLabel.HorizontalOptions = LayoutOptions.StartAndExpand;
                    titleLabel.VerticalOptions = LayoutOptions.EndAndExpand;
                    //titleLabel.BackgroundColor = Color.Blue;
                    //  titleLabel.BackgroundColor = Color.Blue;

                    var verticalview = new Label { HeightRequest = 100, WidthRequest = 5, BackgroundColor = Color.FromHex("FFFFAC") };
                    var channelIcon = new Image { WidthRequest = 80};

      
  
                    channelIcon.SetBinding(Image.SourceProperty, new Binding("Image"));
                    channelIcon.HorizontalOptions = LayoutOptions.CenterAndExpand;
               


                    return new ViewCell
                    {
                        View = new StackLayout
                        {
                            Padding = new Thickness(0, 1, 1, 5),
                            Orientation = StackOrientation.Horizontal,
                            BackgroundColor = Color.FromHex("603C26"),
                            Children =
                                        {
                                           // header,
                                            verticalview,
                                            channelIcon,
                                            titleLabel
                                        }
                        }
                    };
                }),
                RowHeight = 100,
                HeightRequest = 120
                /*  return new ViewCell
                   {
                       View = new RelativeLayout()
                       {
                           Children =
                          {
                              {itembackground, Constraint.Constant(0), Constraint.Constant(0), Constraint.RelativeToParent((parent) => (parent.Bounds.Width)), Constraint.RelativeToParent((parent) => (parent.Bounds.Height))},
                              {new StackLayout()
                              {
                                                              Spacing = 0,
                              VerticalOptions = LayoutOptions.CenterAndExpand,
                              Orientation =StackOrientation.Vertical,
                              Children = 
                              {
                                channelIcon,titleLabel
                              }}, Constraint.Constant(0), Constraint.Constant(0), Constraint.RelativeToParent((parent) => (parent.Bounds.Width)),Constraint.RelativeToParent((parent) => (parent.Bounds.Height))}
                                
                                
                              {channelIcon, Constraint.Constant(40), Constraint.Constant(20),Constraint.Constant(100), Constraint.Constant(100)},
                              {titleLabel, Constraint.Constant(20)   ,Constraint.RelativeToView(channelIcon,(parent, sibling) => sibling.Bounds.Bottom+5)}
                            
                          }
                       }


                   };
              }),
              RowHeight = 150*/

            };

            listView.SetBinding(ListView.ItemsSourceProperty, new Binding("Channels"));
            listView.SetBinding(ListView.SelectedItemProperty, new Binding("SelectedChannel", BindingMode.TwoWay));
            listView.BackgroundColor = Color.FromHex("5b3721");
            listView.ItemSelected += (sender, e) =>
            {
                if (e.SelectedItem == null) return; // don't do anything if we just de-selected the row
                // do something with e.SelectedItem
                ((ListView)sender).SelectedItem = null; // de-select the row
            };

            //listView.BackgroundColor = Color.FromHex("FFAE45");

            var content = new RelativeLayout();

            content.Children.Add(top, Constraint.Constant(0), Constraint.Constant(0), Constraint.RelativeToParent((parent) => parent.Bounds.Width));

            var channelList = new StackLayout
            {
                Padding = new Thickness(8, 8, 8, 8),
                Children =
                {
                    listView
                }
            };

            //    content.Children.Add(bottom, Constraint.Constant(0), Constraint.RelativeToParent((parent) => (parent.Bounds.Height - (double)70)));

            content.Children.Add(channelList, Constraint.Constant(0), Constraint.RelativeToView(top, (parent, sibling) => sibling.Bounds.Bottom), Constraint.RelativeToParent((parent) => parent.Bounds.Width), Constraint.RelativeToView(top, (parent, sibling) => parent.Height - sibling.Height));

            /*   content.Children.Add(bottom, 
                   Constraint.Constant(0),
                   Constraint.RelativeToParent((parent) => (parent.Bounds.Height - bottom.Height)), Constraint.RelativeToParent((parent) => parent.Bounds.Width));*/



            content.BackgroundColor = Color.FromHex("5b3721");
            Content = content;
            NavigationPage.SetHasNavigationBar(this, false);
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            /*  if (listView != null)
              {
                  listView.SelectedItem = null;
              }*/
        }
    }
}
